#ifndef STUDENT_LIB_H_
#define STUDENT_LIB_H_

#include "Student.h"
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>

void Add_Student(string s, Student l[], int &numberStudent){
   string temp, a_fname, a_dob, a_addr, a_classname, a_program;
   int a_id, a_year;

   int mark = s.find(":");
   a_id = atoi(s.substr(0,mark).c_str());


   temp = s.substr(mark+1);
   mark = temp.find(":");
   a_fname = temp.substr(0,mark);

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_dob = temp.substr(0,mark);

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_addr = temp.substr(0,mark);

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_year = atoi(temp.substr(0,mark).c_str());

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_program = temp.substr(0,mark);

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_classname = temp.substr(0,mark);

   Student Stu(a_id, a_fname, a_dob, a_addr, a_year, a_program, a_classname);
   l[numberStudent] = Stu;
   numberStudent = numberStudent + 1;
}
void ReadStudent(string _file, Student l[], int &numberStudent){
               ifstream fin;
               fin.open(_file.c_str());
               if(!fin.is_open()){
                       cout<<"Cannot open file.\n";
                       exit(1);
               }else{
                              string s = "";
                              while (getline(fin, s)){
                                Add_Student(s,l,numberStudent);
                              }
                              fin.close();
               }
}

void WriteStudent(string _file, Student l[], int &numberStudent){
               ofstream fout;
fout.open(_file.c_str());
               for (int i=0 ; i<numberStudent ; i++){
                              fout<<l[i].get_id()<<":"<<l[i].get_fname()<<":"<<l[i].get_dob()<<":"<<l[i].get_addr()<<":"<<l[i].get_year()<<":"<<l[i].get_program()<<":"<<l[i].get_classname()<<endl;
               }
               fout.close();
}

void Search_by_name(string key, string _file, int &k){
     Student b_l[100];
     int i=0,j=0;
     ReadStudent(_file,b_l,i);

     for (j = 0; j < i; j++){
         if (b_l[j].get_fname().find(key) != std::string::npos){
            b_l[j].Show();
            k++;
         }
     }
}

void Search_by_classname(string key, string _file, int &k){
     Student b_l[100];
     int i=0,j=0;
     ReadStudent(_file,b_l,i);
     for (j = 0; j < i; j++){
         if (b_l[j].get_classname() == key){
            b_l[j].Show();
            k++;
         }
     }
}

void Search_by_year(int key, string _file, int &k){
     Student b_l[100];
     int i=0,j=0;
     ReadStudent(_file,b_l,i);
     for (j = 0; j < i; j++){
         if (b_l[j].get_year() == key){
            b_l[j].Show();
            k++;
         }
     }
}

int Search_by_id(int key, Student r_l[], int &k){
	int j=0;
	for(j = 0; j < k; j++)
	{
		if(r_l[j].get_id() == key)
		{
			return j;
		}
	}
	return -1;
}

int FindIdConflict(Student key, Student r_l[], int k){
	int j=0;
	int inf = key.get_id();
	for(j = 0; j < k; j++)
	{
		if(r_l[j].get_id() == inf)
		{
			return -1;
		}
	}
	return 1;
}

int FindConjunction(Student key, Student r_l[], int k){

	int j=0;
	string inf = key.getInfo();

	for(j = 0; j < k; j++)
	{
		if(strcmp(r_l[j].getInfo().c_str(), inf.c_str()) == 0)
		{
			return -1;
		}
	}
	return 1;
}

#endif

